<template>
  <el-dialog
    :title="title"
    width="500px"
    :visible="showDialog"
    @close="close"
  >
    <el-form ref="form" :model="dept" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="dept.name" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="dept.code" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="dept.managerId" placeholder="请选择负责人" style="width: 100%;">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="dept.introduce" type="textarea" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="confirm">确定</el-button>
        <el-button size="mini" @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getDepartmentList, getManagerList, addDepartment, getDepartmentDetail, updateDepartment } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      dept: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let res = await getDepartmentList()
              if (this.dept.id) {
                // 编辑时，排除自身
                res = res.filter(item => item.id !== this.dept.id)
                console.log(res)
              }
              if (res.some(item => item.name === value)) {
                callback(new Error('部门名称重复'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let res = await getDepartmentList()
              if (this.dept.id) {
                res = res.filter(item => item.id !== this.dept.id)
              }
              console.log(res)
              if (res.some(item => item.code === value)) {
                callback(new Error('部门编码重复'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.dept.id ? '编辑部门' : '添加部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    cancel() {
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      this.managerList = await getManagerList()
    },
    async getDepartmentDetail() {
      const res = await getDepartmentDetail(this.currentNodeId)
      this.dept = res
    },
    async confirm() {
      await this.$refs.form.validate()
      if (this.dept.id) {
        await updateDepartment(this.dept)
        this.$message.success('修改成功')
      } else {
        await addDepartment({ ...this.dept, pid: this.currentNodeId })
        this.$message.success('添加成功')
      }
      this.$emit('updateDepartment')
      this.close()
    },
    close() {
      this.dept = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
    }
  }
}
</script>
